﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Base;

namespace HRM
{
    public partial class FormCalculatePayroll : FormBaseData
    {
        private Ctrl_General mCtrlGeneral;

        public FormCalculatePayroll()
        {
            InitializeComponent();
            CreateTextBoxButton(txtDept);
            CreateTextBoxButton(txtIDEmp);
        }

        protected override void BasicForm_Load(object sender, EventArgs e)
        {
            mFormRight = mMainForm.mUserRight.GetAccess(this.Name);
            //base.BasicForm_Load(sender, e);
            if (!mFormRight.mb_read)
            {	// Only show the empty form, no data
                MessageBox.Show("Access denied '"+this.Text+" '. Please contact Administrator HRM System!", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                SetDataManipulation(false);
                this.Close();
                return;
            }
            DateTime dtm = DateTime.Now.AddMonths(-1);
            cboYear.Text = dtm.Year.ToString();
            cboMonth.Text = (dtm.Month.ToString().Length == 1 ? "0" + dtm.Month.ToString() : dtm.Month.ToString());
            if (mCtrlGeneral == null) mCtrlGeneral = new Ctrl_General();
        }

        protected override void BaseForm_Activated(object sender, System.EventArgs e)
        {
            base.BaseForm_Activated(sender, e);
        }

        protected override void BaseForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            base.BaseForm_KeyDown(sender, e);
        }

        protected override void BasicForm_Closing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            base.BasicForm_Closing(sender, e);
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor; 
            mFormRight = mMainForm.mUserRight.GetAccess(this.Name);
            if (!CheckSecurity(this.Name)) return;
            string lsErrorMessage = mCtrlGeneral.CheckPeriod(int.Parse(cboMonth.Text.Trim()), int.Parse(cboYear.Text.Trim()));
            if (!lsErrorMessage.Equals(""))
            {
                MessageBox.Show(lsErrorMessage, ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            string lsReturnMsg;
            // Calculate Time Sheet
            if (chkTimeSheet.Checked)
            {
                lsReturnMsg = string.Empty;
                lsReturnMsg = mCtrlGeneral.CalculateTimeSheet(int.Parse(cboMonth.Text.Trim()), int.Parse(cboYear.Text.Trim()), txtDept.Text.Trim(), txtIDEmp.Text.Trim(), mMainForm.ms_UserID);
                if (lsReturnMsg.Equals("Successful"))
                {
                    MessageBox.Show("Calculate Time Sheet Successful", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    MessageBox.Show(lsReturnMsg, ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    Cursor = Cursors.Default;
                    return;
                }
            }
            // Calculate Payroll
            if (chkCalSal.Checked)
            {
                lsReturnMsg = string.Empty;
                lsReturnMsg = mCtrlGeneral.CalculatePY(int.Parse(cboMonth.Text.Trim()), int.Parse(cboYear.Text.Trim()), txtDept.Text.Trim(), txtIDEmp.Text.Trim(), mMainForm.ms_UserID);
                if (lsReturnMsg.Equals("Successful"))
                {
                    MessageBox.Show("Calculate Payroll Successful", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                    MessageBox.Show(lsReturnMsg, ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            Cursor = Cursors.Default;
        }

        public override void btn_Click(object sender, EventArgs e)
        {
            switch (((Button)sender).Name)
            {
                case "btnIDEmp":
                    OpenSelection("txtIDEmp");
                    break;

                case "btnDept":
                    OpenSelection("txtDept");
                    break;
            }
        }

        #region Open selection
        public override void OpenSelection(string as_SelectioField)
        {
            SelectionParam lstr_param = new SelectionParam();
            lstr_param.ms_SelectionField = as_SelectioField;
            FormSelection lfrmSelect = new FormSelection();
            lfrmSelect.mfrmParentForm = this;
            lfrmSelect.Owner = mMainForm;

            switch (as_SelectioField)
            {
                case "txtIDEmp":
                    lstr_param.ms_SelectionName = "Personnel Profile";
                    lfrmSelect.SetParam(lstr_param);
                    if (lfrmSelect.ShowDialog() == DialogResult.OK)
                    {
                        txtIDEmp.Text = lfrmSelect.GetParam().msa_ReturnValues[0];
                        lblEmpName.Text = lfrmSelect.GetParam().msa_ReturnValues[1];
                    } break;

                case "txtDept":
                    lstr_param.ms_SelectionName = "Department";
                    lfrmSelect.SetParam(lstr_param);
                    if (lfrmSelect.ShowDialog() == DialogResult.OK)
                    {
                        txtDept.Text = lfrmSelect.GetParam().msa_ReturnValues[0];
                        lblDeptName.Text = lfrmSelect.GetParam().msa_ReturnValues[1];
                    } break;
            }
        }
        #endregion

    }
}
